package com.jing.cloud.service.chat.dao.impl;

import com.google.common.collect.Maps;
import com.jing.cloud.service.chat.bean.UserMessage;
import com.jing.cloud.service.chat.dao.UserMessageDao;
import com.jing.cloud.service.dao.impl.DbDaoImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Map;

@Repository
@Slf4j
public class UserMessageDaoImpl extends DbDaoImpl<UserMessage> implements UserMessageDao {

    @Override
    public int updateForce(UserMessage userMessage, int tryTime) {
        int fitch = 0;
        Map<String, Object> condition = Maps.newHashMap();
        condition.put("userId", userMessage.getUserId());
        condition.put("messageId", userMessage.getMessageId());
        userMessage.setUserId(null);
        userMessage.setMessageId(null);
        userMessage.setScene(null);
        userMessage.setRoom(null);
        userMessage.forUpdate();
        for (int i = 0; i < tryTime; i++) {
            fitch = update(userMessage, condition);
            if (fitch > 0) {
                break;
            } else {
                try {
                    Thread.sleep(3000L);
                } catch (Exception e) {
                    log.error("thread sleep error", e);
                }
            }
        }
        return fitch;
    }
}
